﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

/// <summary>
///Check 的摘要说明
/// </summary>
public class Check
{
	public Check()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
    }


    public static members LoginCheck(string username,string password)//用户名密码是否合法
    {
        string pwd = ConvertData.ToMD5(password);
        GetData gd = new GetData();
        members m = gd.GetOneMember(username);
        if (m == null)
            return m;
        else
        {
            if (m.password == pwd)
                return m;
            else
                m = null;
        }
        return m;
    }

    public static bool IsUsernameRepeat(string username)//注册用户名是否重复
    {
        GetData gd = new GetData();
        members m = gd.GetOneMember(username);
        if (m == null)
            return true;
        else
            return false;
    }

    public static bool IsNicknameRepeat(string nickname)//昵称是否重复
    {
        GetData gd = new GetData();
        members m = gd.GetOneMember(nickname);
        if (m == null)
            return true;
        else
            return false;
    }

    public static bool IsUsernameLegal(string username)//检测用户名是否合法
    {
        /*
        if (username.Length < 6 || username.Length > 16)
            return false;
        for (int i = 0; i < username.Length; i++)
        {
            char c = username[i];
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                continue;
            else
                return false;
        }
        return true;
         */
        Regex rName = new Regex(@"^[a-zA-Z0-9_]{4,16}$");
        return rName.IsMatch(username);
    }

    public static bool IsNicknameLegal(string nickname)//检测用户昵称是否合法
    {
        /*
        if (nickname.Length < 6 || nickname.Length > 16)
            return false;
        for (int i = 0; i < nickname.Length; i++)
        {
            char c = nickname[i];
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                continue;
            else
                return false;
        }
        return true;
         */
        Regex rName = new Regex(@"^([a-zA-Z0-9_]{3,16})|([\u0391-\uFFE5]{1,8})$");
        return rName.IsMatch(nickname);
    }

    public static bool IsPasswordLegal(string password)//密码是否符合规范
    {
        /*
        if (password.Length < 6 || password.Length > 16)
            return false;
        for(int i=0;i<password.Length;i++)
        {
            char c=password[i];
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                continue;
            else
                return false;
        }
        return true;
         */

        Regex r = new Regex(@"^[a-zA-Z0-9\W]{6,16}$");
        return r.IsMatch(password);
    }

    public static int CheckToInt(string id)//转换为正整数，失败则返回-1
    {
        int res = 0;
        for(int i=0;i<id.Length;i++)
        {
            char c = id[i];
            if (c < '0' || c > '9')
                return -1;
            res = res * 10 + c - '0';
            if (res < 0 || res > int.MaxValue)
                return -1;
        }
        return res;
    }

    public static bool CheckBirthFormat(string s)//查看日期合法性
    {
        /*
        int i;
        int n = 0;
        int []p=new int[2];
        int[]bir = new int[3];

        for(i=0;i<s.Length;++i)
        {
            if (s[i] == '-') 
            {
                p[n]=i; 
                n += 1;
                continue;
            }
            else if (s[i] >= '0' && s[i] <= '9') continue;
            return false;
        }
        if (n!=2)
        {
            return false;
        }
        if (p[0]==0 || p[1]==s.Length-1 || p[0]==p[1]-1)
        {
            return false;
        }
        int r = 0,len=0;
        for(i=0;i<s.Length;++i)
        {
            if(s[i]!='-')
            {
                r *= 10;
                r += s[i] - '0';
            }else
            {
                bir[len++] = r;
                r = 0;
            }
        }
        bir[2] = r;
        if (bir[1]>12 || bir[1]<1)
        {
            return false;
        }
        if (bir[2] > 31 || bir[2] < 1)
        {
            return false;
        }
        return true;
         */

        Regex r = new Regex(@"^([0-9]{4})-((0?([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$");
        return r.IsMatch(s);
    }

    public static bool CheckGenderLegal(string gender)//查看性别输入合法
    {
        Regex r = new Regex(@"^[男女]{1}$");
        return r.IsMatch(gender);
    }

    public static bool CheckQQLegal(string qq)//检查QQ号码合法性
    {
        Regex r = new Regex(@"^[1-9]{1}[0-9]{4,15}$");
        return r.IsMatch(qq);
    }

    public static bool CheckMailLegal(string mail)//检查email合法性
    {
        Regex r = new Regex(@"^[a-z0-9]{5,16}@[a-z0-9]{3,8}\.(com|net|org|edu)(\.[a-z0-9]{2,4})?$");
        return r.IsMatch(mail);
    }


    public static bool CheckPostLegal(string str)
    {
        if (string.IsNullOrWhiteSpace(str))
            return false;
        if (str.Length < 2 || str.Length > 4000)
            return false;
        else
            return true;
    }

    public static bool CheckTopicTitle(string str)
    {
        if (string.IsNullOrWhiteSpace(str))
            return false;
        if (str.Length < 2 || str.Length > 50)
            return false;
        else
            return true;
    }
}